<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="clearSearchForm">清空</el-button>
      <el-button type="primary" v-if="permissions.admin_pvmteacher_add" icon="el-icon-plus" @click="addOrUpdateHandle('','add')">新增</el-button>
    </el-button-group>
    <el-form inline :model="dataForm" v-if="searchFormShow">
      <el-row class="bg-row">
        <el-col :span="6">
          <el-form-item label="教师编号">
            <el-input v-model="dataForm.teacherCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="教师名称">
            <el-input v-model="dataForm.teacherName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年龄">
            <el-input v-model="dataForm.birthDay"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别">
            <el-select v-model="dataForm.gender">
              <el-option :value="0" label="男"></el-option>
              <el-option :value="1" label="女"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="bg-row">
        <el-col :span="6">
          <el-form-item label="手机号">
            <el-input v-model="dataForm.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="职位名称">
            <el-input v-model="dataForm.positionName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataList" height="calc(100vh - 215px)" v-loading="dataListLoading">
      <el-table-column prop="teacherCode" label="教师编号" width="80"></el-table-column>
      <el-table-column prop="teacherName" label="教师名称" width="100"></el-table-column>
      <el-table-column prop="birthDay" label="年龄" width="120"></el-table-column>
      <el-table-column prop="gender" label="性别" width="120">
        <template slot-scope="scope">
          <el-tag effect="dark" v-if="scope.row.gender == 0">男</el-tag>
          <el-tag effect="dark" type="success" v-if="scope.row.gender == 1">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="120"></el-table-column>
      <el-table-column prop="schoolId" label="学校编号" width="180"></el-table-column>
      <el-table-column prop="deptId" label="部门编号" min-width="180"></el-table-column>
      <el-table-column prop="positionName" label="职位名称" min-width="80">
        <template slot-scope="scope">
          <el-tag effect="dark">{{scope.row.positionName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="230" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-view" @click="addOrUpdateHandle(scope.row.id,'check')">详情</el-button>
          <el-button v-if="permissions.admin_pvmteacher_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'edit')">编辑</el-button>
          <el-button v-if="permissions.admin_pvmteacher_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import {
    fetchList,
    getObj,
    addObj,
    putObj,
    delObj
  } from '@/api/pvm/teacher'
  import { mapGetters } from 'vuex'

  import TableForm from './TableForm'

  export default {
    name: 'pvmteacher',
    data() {
      return {
        searchFormShow: true,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataForm: {},
        addOrUpdateVisible: false,
      }
    },
    components: {
      TableForm
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    created() {
      this.searchHandle()
    },
    methods: {
      // 查询教师数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      // 获取教师数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        fetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页条数改变
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页改变
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 打开弹窗 新增 / 修改 / 详情
      addOrUpdateHandle(id, type) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, type)
        })

      },
      // 删除教师
      deleteHandle(row) {
        this.$confirm('此操作将永久删除该教师(教师名为:' + row.teacherName + '), 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  转换类型格式
      statusFormat(val) {
        let obj = {}
        obj = this.statusOption.find(item => {
          if (item.value === val) {
            return item
          }
        })
        if (obj && obj.label) {
          return obj.label
        }
      }
    }
  }
</script>
